<template>
  <div class="avue-top">
    <div class="top-bar__left">
      <div class="avue-breadcrumb"
           :class="[{ 'avue-breadcrumb--active': isCollapse }]"
           v-if="showCollapse">
        <i class="icon-navicon"
           @click="setCollapse"></i>
      </div>
    </div>
    <div class="top-bar__title">
      <div class="top-bar__item top-bar__item--show"
           v-if="showMenu">
        <top-menu ref="topMenu"></top-menu>
      </div>
      <span class="top-bar__item"
            v-if="showSearch">
        <top-search></top-search>
      </span>
    </div>
    <div class="top-bar__right">
      <div v-if="showSelectApp" class="showSelectApp">
        <span>应用：</span>
        <el-select size="mini" v-model="appId" @change="changeApp(appId)">
          <template v-for="{nameCn,oid} in applicationList">
            <el-option :label="nameCn" :key="oid" :value="oid"></el-option>
          </template>
        </el-select>
      </div>
      <el-tooltip v-if="showColor"
                  effect="dark"
                  :content="$t('navbar.color')"
                  placement="bottom">
        <div class="top-bar__item">
          <top-color></top-color>
        </div>
      </el-tooltip>
      <el-tooltip v-if="showDebug"
                  effect="dark"
                  :content="logsFlag?$t('navbar.bug'):logsLen+$t('navbar.bugs')"
                  placement="bottom">
        <div class="top-bar__item">
          <top-logs></top-logs>
        </div>
      </el-tooltip>
      <el-tooltip v-if="showLock"
                  effect="dark"
                  :content="$t('navbar.lock')"
                  placement="bottom">
        <div class="top-bar__item">
          <top-lock></top-lock>
        </div>
      </el-tooltip>
      <el-tooltip v-if="showTheme"
                  effect="dark"
                  :content="$t('navbar.theme')"
                  placement="bottom">
        <div class="top-bar__item top-bar__item--show">
          <top-theme></top-theme>
        </div>
      </el-tooltip>
      <el-tooltip effect="dark"
                  v-if="false"
                  :content="$t('navbar.notice')"
                  placement="bottom">
        <div class="top-bar__item top-bar__item--show">
          <top-notice></top-notice>
        </div>
      </el-tooltip>
      <el-tooltip effect="dark"
                  v-if="false"
                  :content="$t('navbar.language')"
                  placement="bottom">
        <div class="top-bar__item top-bar__item--show">
          <top-lang></top-lang>
        </div>
      </el-tooltip>
      <el-tooltip v-if="showFullScren"
                  effect="dark"
                  :content="isFullScren?$t('navbar.screenfullF'):$t('navbar.screenfull')"
                  placement="bottom">
        <div class="top-bar__item">
          <i :class="isFullScren?'icon-tuichuquanping':'icon-quanping'"
             @click="handleScreen"></i>
        </div>
      </el-tooltip>
      <img class="top-bar__img"
           :src="userInfo.avatar">
      <el-dropdown>
        <span class="el-dropdown-link">
         {{ userInfo.userName }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>
            <router-link to="/">{{ $t('navbar.dashboard') }}</router-link>
          </el-dropdown-item>
          <el-dropdown-item>
            <router-link to="/info/index">{{ $t('navbar.userinfo') }}</router-link>
          </el-dropdown-item>
          <el-dropdown-item @click.native="switchDept"
          >{{ $t('navbar.switchDept') }}
          </el-dropdown-item>
          <el-dropdown-item @click.native="logout"
                            divided>{{ $t('navbar.logOut') }}
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dialog title="用户信息选择"
                 append-to-body
                 :visible.sync="userBox"
                 width="350px">
        <avue-form ref="form" :option="userOption" v-model="userForm" @submit="submitSwitch"/>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {resetRouter} from '@/router/router'
import {mapGetters, mapState} from "vuex";
import {fullscreenToggel, listenfullscreen} from "@/util/util";
import topLock from "./top-lock";
import topMenu from "./top-menu";
import topSearch from "./top-search";
import topTheme from "./top-theme";
import topLogs from "./top-logs";
import topColor from "./top-color";
import topNotice from './top-notice'
import topLang from "./top-lang";
import * as application from "@/api/application";
import {getToken} from "@/util/auth";
import website from "@/config/website";
import {EventBus} from "@/components/javaScript/EventBus";
import {request} from "axios";

export default {
  components: {
    topLock,
    topMenu,
    topSearch,
    topTheme,
    topLogs,
    topColor,
    topNotice,
    topLang
  },
  name: "top",
  data() {
    return {
      userBox: false,
      userForm: {
        deptId: '',
        roleId: ''
      },
      appId: 1,
      userOption: {
        labelWidth: 70,
        submitBtn: true,
        emptyBtn: false,
        submitText: '切换',
        column: [
          {
            label: '部门',
            prop: 'deptId',
            type: 'select',
            props: {
              label: 'deptName',
              value: 'id'
            },
            dicUrl: '/api/blade-system/dept/select',
            span: 24,
            display: false,
            rules: [{
              required: true,
              message: "请选择部门",
              trigger: "blur"
            }],
          },
          {
            label: '角色',
            prop: 'roleId',
            type: 'select',
            props: {
              label: 'roleName',
              value: 'id'
            },
            dicUrl: '/api/blade-system/role/select',
            span: 24,
            display: false,
            rules: [{
              required: true,
              message: "请选择角色",
              trigger: "blur"
            }],
          },
        ]
      },
      applicationList: []
    };
  },
  filters: {},
  created() {
    this.appId = localStorage.getItem("appId") + '';
  },
  mounted() {
    let that = this;
    this.getApplicationList();
    document.addEventListener("haveToken", function () {
      that.getApplicationList();
    });

    listenfullscreen(this.setScreen);
  },
  computed: {
    ...mapState({
      showDebug: state => state.common.showDebug,
      showTheme: state => state.common.showTheme,
      showLock: state => state.common.showLock,
      showFullScren: state => state.common.showFullScren,
      showCollapse: state => state.common.showCollapse,
      showSearch: state => state.common.showSearch,
      showMenu: state => state.common.showMenu,
      showColor: state => state.common.showColor,
      showSelectApp: state => state.common.showSelectApp
    }),
    ...mapGetters([
      "userInfo",
      "isFullScren",
      "tagWel",
      "tagList",
      "isCollapse",
      "tag",
      "logsLen",
      "logsFlag"
    ])
  },
  methods: {
    handleScreen() {
      fullscreenToggel();
    },
    setCollapse() {
      this.$store.commit("SET_COLLAPSE");
    },
    setScreen() {
      this.$store.commit("SET_FULLSCREN");
    },
    switchDept() {
      const userId = this.userInfo.user_id;
      const deptColumn = this.findObject(this.userOption.column, "deptId");
      deptColumn.dicUrl = `/api/blade-system/dept/select?userId=${userId}`;
      deptColumn.display = true;
      const roleColumn = this.findObject(this.userOption.column, "roleId");
      roleColumn.dicUrl = `/api/blade-system/role/select?userId=${userId}`;
      roleColumn.display = true;
      this.userBox = true;
    },
    submitSwitch(form, done) {
      this.$store.dispatch("refreshToken", form).then(() => {
        this.userBox = false;
        this.$router.push({path: "/"});
      })
      done();
    },
    logout() {
      this.$confirm(this.$t("logoutTip"), this.$t("tip"), {
        confirmButtonText: this.$t("submitText"),
        cancelButtonText: this.$t("cancelText"),
        type: "warning"
      }).then(() => {
        this.$store.dispatch("LogOut").then(() => {
          this.clearCookies();
          localStorage.clear();
          resetRouter();
          if (process.env.NODE_ENV === 'development') {
            window.open(process.env.VUE_APP_LOGIN_URL + website.ssoUrl + website.redirectUri, "_self")
          } else {
            request({url: "/api/manager-api/url/getAuthServiceUrl", method: "get"}).then((res) => {
              let baseUrl = res.data.split(",")[1];
              window.open(baseUrl + website.ssoUrl + website.redirectUri, "_self")
            })
          }
        });
      });
    },
    clearCookies() {
      let cookies = document.cookie.split(";");

      for (let i = 0; i < cookies.length; i++) {
        let cookie = cookies[i];
        let eqPos = cookie.indexOf("=");
        let name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
      }
    },
    getApplicationList() {
      if (getToken()) {
        application.getlist().then(res => {
          this.$store.commit("SET_APPLICATION_LIST", res.data.data);
          this.applicationList = res.data.data;
          const defaultApp = res.data.data[0] || {};
          const defaultAppid = localStorage.getItem("appId");
          this.setLocalStorage(defaultAppid || defaultApp.oid);
          this.appId = defaultAppid || defaultApp.oid;
        })
      }
    },
    setLocalStorage(id) {
      localStorage.setItem("appId", id)
      const [selectApp] = this.applicationList.filter(v => v.oid === id);
      if (selectApp) {
        localStorage.setItem("defaultFlag", selectApp.defaultFlag)
      }
    },
    changeApp(id) {
      this.setLocalStorage(id);
      EventBus.$emit("changApplication", id)
    }
  }
};
</script>

<style lang="scss" scoped>
.showSelectApp {
  font-size: 14px;
  font-weight: normal;
  margin-right: 10px;

  ::v-deep .el-select {
    font-size: 14px;

    .el-input {
      border: none;
      background: rgba(0, 0, 0, 0);
      width: 120px;

      .el-input__inner {
        border: none;
        border-radius: 0;
        background: rgba(0, 0, 0, 0);
        width: 120px;
        border-bottom: 1px solid #C0C4CC;

        &::placeholder {
          font-size: 14px;
        }
      }
    }
  }
}
</style>
